//
//  HYPreviewImageController.m
//  XPlayer
//
//  Created by 张水 on 2023/9/20.
//

#import "HYPreviewImageController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <HYUIKit/HYAlertShowAnimate.h>
#import <Photos/Photos.h>
#import "HYHub.h"
#import "HYImageCollectionCell.h"

@interface HYPreviewImageController ()
<
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout,
HYPreviewImageScrollViewDelegate
>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, assign) NSInteger  currentIndex;

@property (nonatomic, strong) NSArray  *imageURLs;
@end

@implementation HYPreviewImageController

- (instancetype)initWithImages:(NSArray *)images currentIndex:(NSInteger)currentIndex {
    if (self = [super init]) {
        self.imageURLs = images;
        self.currentIndex = currentIndex;
    }
    return self;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = [NSString stringWithFormat:@"%ld/%ld",_currentIndex + 1 , self.imageURLs.count];
    self.navigationBarView.titleLabel.textColor = APPMainThemeColor;
    self.view.backgroundColor = AppBgThemeColor;
    self.navigationBarView.navagationBarStyle = Left_button_Show;
    self.navigationBarView.backgroundColor = UIColor.clearColor;
    [self.navigationBarView.leftButton setImage:[UIImage imageNamed:@"nav_btn_close_n"] forState:UIControlStateNormal];
    //等图标出来后换个白色的X
    [self configUI];
    [self.view bringSubviewToFront:self.navigationBarView];
}

- (void)configUI
{
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    @weakify(self);
    [[[[[[[RACObserve(self.collectionView, contentSize) distinctUntilChanged] filter:^BOOL(id value) {
                                return [value CGSizeValue].height>0;
            }] take:1] delay:0.1]deliverOn:[RACScheduler mainThreadScheduler]]takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
                @strongify(self);
                [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
            }];
}

- (void)leftButtonClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UICollectionViewDataSource
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{
    return CGSizeMake(kScreenWidth, kScreenHeight);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageURLs.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HYImageCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CEllIDHYImageCollectionCell" forIndexPath:indexPath];
    cell.scrollView.HYPreViewdelegate = self;
    cell.scrollView.imagePath = self.imageURLs[indexPath.item];
    cell.scrollView.zoomScale = 1.0;
    return cell;
}

#pragma mark - UICollectionViewDelagate 当图图片滑出屏幕外时，将图片比例重置为原始比例
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath 
{
    HYImageCollectionCell *Cell = (HYImageCollectionCell *)cell;
    Cell.scrollView.zoomScale = 1.0;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{
    _currentIndex = (long)scrollView.contentOffset.x/self.view.bounds.size.width;
    self.navigationBarView.titleLabel.text = [NSString stringWithFormat:@"%ld/%ld",_currentIndex+1,self.imageURLs.count];
}

- (void)singleClickImageWith:(HYPreviewImageScrollView *)scrollView
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        [_collectionView registerClass:[HYImageCollectionCell class] forCellWithReuseIdentifier:@"CEllIDHYImageCollectionCell"];
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    return _collectionView;
}
@end
